﻿using BCMongoEntity.StatisticsRequest;
using BCService;
using BCService.Kafka;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BCTMSRESTful.Configs
{
    /// <summary>
    /// 统计请求
    /// </summary>
    public class StatisticsRequestAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 接口名称
        /// </summary>
        private string name;
        /// <summary>
        /// 接口地址
        /// </summary>
        private string url;
        /// <summary>
        /// 参数列表
        /// </summary>
        private IDictionary<string, object> actionArguments;

        private readonly IConfiguration _configuration;
        private readonly IApplicationContextService _applicationContextService;
        private readonly IKafkaProducerService _kafkaProducerService;
        public StatisticsRequestAttribute(IConfiguration configuration, IApplicationContextService applicationContextService, IKafkaProducerService kafkaProducerService)
        {
            _configuration = configuration;
            _applicationContextService = applicationContextService;
            _kafkaProducerService = kafkaProducerService;
        }

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            DateTime date = DateTime.Now;
            StatisticsRequestEntity entity = new StatisticsRequestEntity
            {
                AccessApiName = name,
                AccessApiUrl = url,
                Parameters = JsonConvert.SerializeObject(actionArguments.ToDictionary(key => key.Key, value => value.Value)),
                Year = date.Year,
                Month = date.Month,
                Day = date.Day,
                ControllerName = context.ActionDescriptor.RouteValues["controller"],
                ActionName = context.ActionDescriptor.RouteValues["action"],
                CreateTime = date,
                UpdateTime = date
            };
            if (bool.TryParse(_configuration["EnabledStatisticsRequest"], out bool statisticsRequestEnabled))
            {
                if (statisticsRequestEnabled)
                {
                    if (context.Exception != null)
                    {
                        entity.ErrorTimes = 1;
                        _kafkaProducerService.Send(entity, "StatisticsRequest");
                        return;
                    }

                    entity.SuccessTimes = 1;
                    _kafkaProducerService.Send(entity, "StatisticsRequest");
                }
            }
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            url = context.HttpContext.Request.Path.Value;
            name = context.ActionDescriptor.DisplayName;
            actionArguments = context.ActionArguments;
#if DEBUG
            Console.WriteLine("{0},{1},{2}", url, name, JsonConvert.SerializeObject(actionArguments.ToDictionary(key => key.Key, value => value.Value)));
#endif
        }
    }
}
